package cn.seaboot.admin.mybatis;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.Properties;

/**
 * page-helper 参数配置类
 * <p>
 * 配置方式与 page-helper-spring-boot-starter-1.4.7 完全一致，
 * mybatis的自动启动，是我们调整过的。
 *
 * @author Mr.css
 * @version 2021-02-01 14:52
 */
@Component
@ConfigurationProperties(prefix = PageHelperProperties.PAGE_HELPER_PREFIX)
public class PageHelperProperties {
    public static final String PAGE_HELPER_PREFIX = "pagehelper";

    private final Properties properties = new Properties();
    private Boolean offsetAsPageNum;
    private Boolean rowBoundsWithCount;
    private Boolean pageSizeZero;
    private Boolean reasonable;
    private Boolean supportMethodsArguments;
    private String dialect;
    private String helperDialect;
    private Boolean autoRuntimeDialect;
    private Boolean autoDialect;
    private Boolean closeConn;
    private String params;
    private Boolean defaultCount;
    private String dialectAlias;
    private String autoDialectClass;
    private Boolean useSqlserver2012;
    private String countColumn;
    private String replaceSql;
    private String sqlCacheClass;
    private String boundSqlInterceptors;
    private Boolean keepOrderBy;
    private Boolean keepSubSelectOrderBy;
    private String sqlParser;

    public PageHelperProperties() {
    }

    public Properties getProperties() {
        return this.properties;
    }

    public Boolean getOffsetAsPageNum() {
        return this.offsetAsPageNum;
    }

    public void setOffsetAsPageNum(Boolean offsetAsPageNum) {
        this.offsetAsPageNum = offsetAsPageNum;
        if (defaultCount != null) {
            this.properties.setProperty("offsetAsPageNum", offsetAsPageNum.toString());
        }
    }

    public Boolean getRowBoundsWithCount() {
        return this.rowBoundsWithCount;
    }

    public void setRowBoundsWithCount(Boolean rowBoundsWithCount) {
        this.rowBoundsWithCount = rowBoundsWithCount;
        if (defaultCount != null) {
            this.properties.setProperty("rowBoundsWithCount", rowBoundsWithCount.toString());
        }
    }

    public Boolean getPageSizeZero() {
        return this.pageSizeZero;
    }

    public void setPageSizeZero(Boolean pageSizeZero) {
        this.pageSizeZero = pageSizeZero;
        if (defaultCount != null) {
            this.properties.setProperty("pageSizeZero", pageSizeZero.toString());
        }
    }

    public Boolean getReasonable() {
        return reasonable;
    }

    public void setReasonable(Boolean reasonable) {
        this.reasonable = reasonable;
        if (defaultCount != null) {
            this.properties.setProperty("reasonable", reasonable.toString());
        }
    }

    public Boolean getSupportMethodsArguments() {
        return this.supportMethodsArguments;
    }

    public void setSupportMethodsArguments(Boolean supportMethodsArguments) {
        this.supportMethodsArguments = supportMethodsArguments;
        if (defaultCount != null) {
            this.properties.setProperty("supportMethodsArguments", supportMethodsArguments.toString());
        }
    }

    public String getDialect() {
        return this.dialect;
    }

    public void setDialect(String dialect) {
        this.dialect = dialect;
        if (defaultCount != null) {
            this.properties.setProperty("dialect", dialect);
        }
    }

    public String getHelperDialect() {
        return this.helperDialect;
    }

    public void setHelperDialect(String helperDialect) {
        this.helperDialect = helperDialect;
        if (defaultCount != null) {
            this.properties.setProperty("helperDialect", helperDialect);
        }
    }

    public Boolean getAutoRuntimeDialect() {
        return this.autoRuntimeDialect;
    }

    public void setAutoRuntimeDialect(Boolean autoRuntimeDialect) {
        this.autoRuntimeDialect = autoRuntimeDialect;
        if (defaultCount != null) {
            this.properties.setProperty("autoRuntimeDialect", autoRuntimeDialect.toString());
        }
    }

    public Boolean getAutoDialect() {
        return this.autoDialect;
    }

    public void setAutoDialect(Boolean autoDialect) {
        this.autoDialect = autoDialect;
        if (defaultCount != null) {
            this.properties.setProperty("autoDialect", autoDialect.toString());
        }
    }

    public Boolean getCloseConn() {
        return this.closeConn;
    }

    public void setCloseConn(Boolean closeConn) {
        this.closeConn = closeConn;
        if (defaultCount != null) {
            this.properties.setProperty("closeConn", closeConn.toString());
        }
    }

    public String getParams() {
        return this.params;
    }

    public void setParams(String params) {
        this.params = params;
        if (defaultCount != null) {
            this.properties.setProperty("params", params);
        }
    }

    public Boolean getDefaultCount() {
        return this.defaultCount;
    }

    public void setDefaultCount(Boolean defaultCount) {
        this.defaultCount = defaultCount;
        Optional.ofNullable(defaultCount).ifPresent(v -> properties.setProperty("defaultCount", v.toString()));
    }

    public String getDialectAlias() {
        return this.dialectAlias;
    }

    public void setDialectAlias(String dialectAlias) {
        this.dialectAlias = dialectAlias;
        Optional.ofNullable(dialectAlias).ifPresent(v -> properties.setProperty("dialectAlias", v.toString()));
    }


    public String getAutoDialectClass() {
        return autoDialectClass;
    }

    public void setAutoDialectClass(String autoDialectClass) {
        this.autoDialectClass = autoDialectClass;
        Optional.ofNullable(autoDialectClass).ifPresent(v -> properties.setProperty("autoDialectClass", v.toString()));
    }

    public Boolean getUseSqlserver2012() {
        return useSqlserver2012;
    }

    public void setUseSqlserver2012(Boolean useSqlserver2012) {
        this.useSqlserver2012 = useSqlserver2012;
        Optional.ofNullable(useSqlserver2012).ifPresent(v -> properties.setProperty("useSqlserver2012", v.toString()));
    }

    public String getCountColumn() {
        return countColumn;
    }

    public void setCountColumn(String countColumn) {
        this.countColumn = countColumn;
        Optional.ofNullable(countColumn).ifPresent(v -> properties.setProperty("countColumn", v));
    }

    public String getReplaceSql() {
        return replaceSql;
    }

    public void setReplaceSql(String replaceSql) {
        this.replaceSql = replaceSql;
        Optional.ofNullable(replaceSql).ifPresent(v -> properties.setProperty("replaceSql", v));
    }

    public String getSqlCacheClass() {
        return sqlCacheClass;
    }

    public void setSqlCacheClass(String sqlCacheClass) {
        this.sqlCacheClass = sqlCacheClass;
        Optional.ofNullable(sqlCacheClass).ifPresent(v -> properties.setProperty("sqlCacheClass", v));
    }

    public String getBoundSqlInterceptors() {
        return boundSqlInterceptors;
    }

    public void setBoundSqlInterceptors(String boundSqlInterceptors) {
        this.boundSqlInterceptors = boundSqlInterceptors;
        Optional.ofNullable(boundSqlInterceptors).ifPresent(v -> properties.setProperty("boundSqlInterceptors", v));
    }

    public Boolean getKeepOrderBy() {
        return keepOrderBy;
    }

    public void setKeepOrderBy(Boolean keepOrderBy) {
        this.keepOrderBy = keepOrderBy;
        Optional.ofNullable(keepOrderBy).ifPresent(v -> properties.setProperty("keepOrderBy", v.toString()));
    }

    public Boolean getKeepSubSelectOrderBy() {
        return keepSubSelectOrderBy;
    }

    public void setKeepSubSelectOrderBy(Boolean keepSubSelectOrderBy) {
        this.keepSubSelectOrderBy = keepSubSelectOrderBy;
        Optional.ofNullable(keepSubSelectOrderBy).ifPresent(v -> properties.setProperty("keepSubSelectOrderBy", v.toString()));
    }

    public String getSqlParser() {
        return sqlParser;
    }

    public void setSqlParser(String sqlParser) {
        this.sqlParser = sqlParser;
        Optional.ofNullable(sqlParser).ifPresent(v -> properties.setProperty("sqlParser", v));
    }

    @Override
    public String toString() {
        return "PageHelperProperties{" +
                "offsetAsPageNum=" + offsetAsPageNum +
                ", rowBoundsWithCount=" + rowBoundsWithCount +
                ", pageSizeZero=" + pageSizeZero +
                ", reasonable=" + reasonable +
                ", supportMethodsArguments=" + supportMethodsArguments +
                ", dialect='" + dialect + '\'' +
                ", helperDialect='" + helperDialect + '\'' +
                ", autoRuntimeDialect=" + autoRuntimeDialect +
                ", autoDialect=" + autoDialect +
                ", closeConn=" + closeConn +
                ", params='" + params + '\'' +
                ", defaultCount=" + defaultCount +
                ", dialectAlias='" + dialectAlias + '\'' +
                ", autoDialectClass='" + autoDialectClass + '\'' +
                ", useSqlserver2012=" + useSqlserver2012 +
                ", countColumn='" + countColumn + '\'' +
                ", replaceSql='" + replaceSql + '\'' +
                ", sqlCacheClass='" + sqlCacheClass + '\'' +
                ", boundSqlInterceptors='" + boundSqlInterceptors + '\'' +
                ", keepOrderBy=" + keepOrderBy +
                ", keepSubSelectOrderBy=" + keepSubSelectOrderBy +
                ", sqlParser='" + sqlParser + '\'' +
                '}';
    }
}
